﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using Trax.EntityModel;

namespace Trax.DAL
{
   

    public class TaskOpsDAL : IDataAccess
    {

        string connString;
        SqlDataAdapter AdTask;
        DataSet Ds;

        public TaskOpsDAL()
        {
            connString = System.Configuration.ConfigurationManager.ConnectionStrings["TraxConnection"].ConnectionString.ToString();
        }
        public List<Task> GetTasks()
        {
            List<Task> lstTaskInfo = new List<Task>();
            AdTask = new SqlDataAdapter("Select * from Task", connString);
            Ds = new DataSet();
            AdTask.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            AdTask.Fill(Ds, "Task");
            foreach (DataRow Dr in Ds.Tables["Task"].Rows)
            {
                lstTaskInfo.Add(new Task()
                {
                    Task_ID = Convert.ToInt32(Dr["Task_ID"]),
                    Task_Title = Dr["Task_Title"].ToString(),
                    Task_Description = Dr["Task_Description"].ToString(),
                    Task_Assigned = Dr["Task_Assigned"].ToString(),
                    Task_Start_Date = Convert.ToDateTime(Dr["Task_Start_Date"]),
                    Task_Due_Date = Convert.ToDateTime(Dr["Task_Due_Date"]),
                    Progress =Convert.ToInt32(Dr["Progress"]),
                    Status = Dr["Status"].ToString()
                });
            }
            return lstTaskInfo;
        }
        public Task GetTaskByID(int ID)
        {
            List<Task> lstTaskInfo = new List<Task>();
            AdTask = new SqlDataAdapter("Select * from Task where Task_ID=" + ID.ToString(), connString);
            Ds = new DataSet();
            AdTask.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            AdTask.Fill(Ds, "Task");
            DataRow Dr = Ds.Tables["Task"].Rows[0];
            Task find = new Task();
            find.Task_ID = Convert.ToInt32(Dr["Task_ID"]);
            find.Task_Title = Dr["Task_Title"].ToString();
            find.Task_Description = Dr["Task_Description"].ToString();
            find.Task_Assigned = Dr["Task_Assigned"].ToString();
            find.Task_Start_Date = Convert.ToDateTime(Dr["Task_Start_Date"]);
            find.Task_Due_Date = Convert.ToDateTime(Dr["Task_Due_Date"]);
            find.Progress = Convert.ToInt32(Dr["Progress"]);
            find.Status = Dr["Status"].ToString();
            return find;
        }
        public void CreateTask(Task objTask)
        {

            try
            {
                using (SqlConnection Connection = new SqlConnection(connString))
                {
                    Connection.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Connection;
                    Cmd.CommandText = "Insert into Task Values(@TaskTitle,@TaskDesc, @TaskAssigned,@TaskStartDate,@TaskDueDate,@Prog,@Status)";
                    Cmd.Parameters.AddWithValue("@TaskTitle", objTask.Task_Title);
                    Cmd.Parameters.AddWithValue("@TaskDesc", objTask.Task_Description);
                    Cmd.Parameters.AddWithValue("@TaskAssigned", objTask.Task_Assigned);
                    Cmd.Parameters.AddWithValue("@TaskStartDate", objTask.Task_Start_Date);
                    Cmd.Parameters.AddWithValue("@TaskDueDate", objTask.Task_Due_Date);
                    Cmd.Parameters.AddWithValue("@Prog", objTask.Progress);
                    Cmd.Parameters.AddWithValue("@Status", objTask.Status);
                    Cmd.ExecuteNonQuery();
                    Connection.Close();
                                      
                }
            }
            catch (Exception ex)
            {
                
            }

        }
        public void UpdateTask(Task obj)
        {

            try
            {
                using (SqlConnection SqlConnection = new SqlConnection(connString))
                {

                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = SqlConnection;
                    Cmd.CommandText = "Update Task set Task_Title=@Task_Title, Task_Description=@Task_Description,Task_Assigned=@Task_Assigned,Task_Start_Date=@Task_Start_Date,Task_Due_Date=@Task_Due_Date,Progress=@Progress,Status=@Status where Task_ID=@Task_ID";
                    Cmd.Parameters.AddWithValue("@Task_Title", obj.Task_Title);
                    Cmd.Parameters.AddWithValue("@Task_Description", obj.Task_Description);
                    Cmd.Parameters.AddWithValue("@Task_Assigned", obj.Task_Assigned);
                    Cmd.Parameters.AddWithValue("@Task_Start_Date", obj.Task_Start_Date.ToShortDateString());
                    Cmd.Parameters.AddWithValue("@Task_Due_Date", obj.Task_Due_Date.ToShortDateString());
                    Cmd.Parameters.AddWithValue("@Progress", obj.Progress);
                    Cmd.Parameters.AddWithValue("@Status", obj.Status);
                    Cmd.Parameters.AddWithValue("@Task_ID", obj.Task_ID);
                    SqlConnection.Open();
                    Cmd.ExecuteNonQuery();
                    SqlConnection.Close();
                }
                

            }
            catch (Exception ex)
            {
                
                
            }
        }
        public void DeleteTask(int Task_ID)
        {
            try
            {
                using (SqlConnection Connection = new SqlConnection(connString))
                {
                    Connection.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Connection;
                    Cmd.CommandText = "Delete Task where Task_ID=@Task_ID";
                    Cmd.Parameters.AddWithValue("@Task_ID", Task_ID);
                    Cmd.ExecuteNonQuery();
                    Connection.Close();
                }
            }

            catch (Exception ex)
            {
               
            }
        }
    }
}
